package com.sky.service.impl;

import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.mapper.EmployeeMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtils;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    public EmployeeMapper employeeMapper;
    @Autowired
    public JwtProperties jwtProperties;
    @Override
    public EmployeeLoginVO login(String username,String password) {
        Employee user = employeeMapper.findUser(username);

        // 比对密码是否正确
        String password1 = user.getPassword();
        boolean b = password.equals(password1);
        if(!b){
            throw new RuntimeException("账号或者密码错误");
        }
        // 生产token
        HashMap<String, Object> map = new HashMap<>();
        map.put("empId",user.getId());
        String jwt = JwtUtils.createJwt(map,jwtProperties.getAdminSecretKey(),jwtProperties.getAdminTtl());
        // 设置返回数据
        EmployeeLoginVO vo = new EmployeeLoginVO();
        vo.setId(user.getId());
        vo.setName(user.getName());
        vo.setUserName(user.getUsername());
        vo.setToken(jwt);
        log.info("---{}",Result.success().toString());
        return vo;
    }

    @Override
    public void save(EmployeeDTO emp) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(emp,employee);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setStatus(StatusConstant.ENABLE);
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        Long id = BaseContext.getCurrentId();
        employee.setCreateUser(id);
        employee.setUpdateUser(id);

        employeeMapper.save(employee);
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO queryDTO) {
    PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
    Page<Employee> page = employeeMapper.pageQuery(queryDTO);
        long total = page.getTotal();
        List<Employee> result = page.getResult();
        return new PageResult(total,result);
    }
    /**
     * 设置员工启用禁用状态
     * @param id
     * @param status
     * 0 - 禁用
     * 1 - 启用
     */
    @Override
    public void statusChange(long id, Integer status) {
        // 更新数据库 update employee set status = #{status} where id = #{id}
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus(status);
        employeeMapper.update(employee);
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Override
    public Employee getById(long id) {
        Employee employee = employeeMapper.getById(id);
        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());
        log.info("----->:{}",employee.toString());
        employeeMapper.update(employee);
    }
}
